#! /usr/bin/python3.3


import re, time, logging

def e2iMultiplication(latexExpr):
    """
    converts explicit multiplication to implicit multiplication if first factor is a number
    remove multiplication dot (\cdot) in latex expression
    if the first factor is a number
    """
    print(latexExpr)
    print(re.sub("(\d+) \\\\cdot (\D+)", "\\1 \\2", latexExpr))
    return re.sub("(\d+) \\\\cdot (\D+)", "\\1 \\2", latexExpr)

class StopWatch(object):
    """
    print duration of operations.
    not very accurate
    """
    def __init__(self):
        print(0.0, "s")
        self._start = time.time()
        self._lastMeasure = self._start
    
    def elapsedTime(self):
        now = time.time()
        print(now - self._start, 's\t delta: ', now - self._lastMeasure, 's')
        self._lastMeasure = now


class Verbose(object):
    """
    Superclass for Objects to make them verbose
    """ 
    
    def __getattribute__(self, name):
        
        # using object is needed to prevent infinite loop
        className = object.__getattribute__(self, '__class__').__name__ 
        
        logging.debug('reading %s.%s', className, name)
        
        # actually return requested attribute using object method
        return object.__getattribute__(self, name)
    
    
    def __setattribute__(self, name, *args, **kwargs):
        
        # using object is needed to prevent infinite loop
        className = object.__getattribute__(self, '__class__').__name__ 
        
        logging.info('setting %s.%s with args %s and kwargs %s', className, name, str(args), str(kwargs))
        
        # actually return requested attribute using object method
        return object.__setattribute__(self, name)
        